/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: EWALTERS
 * Created: 2:20:27 PM, Aug 2, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet.analysis.ast;

import java.util.Collection;

import com.mitre.spmif2opnet.VisitorContext;
import com.mitre.spmif2opnet.analysis.ExpandedSymbol;
import com.mitre.spmif2opnet.analysis.Identifier;

/**
 * Our node that represents a graph (node with sub-structure)
 */
public class ExpandedNode extends Node implements Visitee {
	/**
	 * Symbol pointing to the graph that this node represents
	 */
	private ExpandedSymbol _sym;
	
	/**
	 * Default constructor
	 * @param name node name
	 * @param id   node id
	 * @param sym  the symbol
	 * @param prob node probability
	 */
	public ExpandedNode(String name, String id, ExpandedSymbol sym, double prob) {
		super(name, id, prob);
		_sym = sym;
	}

	/**
	 * Visitor acceptor
	 */
	public <T> VisitorContext<T> accept(NodeVisitor<T> v, VisitorContext<T> c) {
		return v.visit(this, c);
	}
	
	/**
	 * What resources are associated with the node?
	 * @return the resource names
	 */
	public Collection<String> getResources() {
		Collection<String> res = super.getResources();
		if (res.isEmpty()) {
			Graph g = _sym.getGraph();
			Node  n = g.getStartNode();
			res = n.getResources();
		}
		return res;
	}
	
	/**
	 * Get the services that correspond to the resource
	 * Use the resources of the start node of the pointed-to graph (if present)
	 * @param res_name the resource name
	 * @return the units
	 */
	public double getResourceService(String res_name) {
		Double service = null;
		if (_resources.isEmpty()) {
			service = new Double(_sym.getGraph().getStartNode().getResourceService(res_name));
		} else {
			service = _resources.get(res_name);
		}
		return (service == null) ? 0.0 : service.doubleValue();
	}

	/**
	 * Copy function
	 * @return the copy
	 */
	public ExpandedNode copy() {
		ExpandedNode new_node = new ExpandedNode(this.getName(), Identifier.newNodeId(this.getId()), _sym, this.getProbability());
		new_node.copyResources(this);
		return new_node;
	}

	/**
	 * Graph accessor
	 * @return the graph
	 */
	public Graph getGraph() {
		return _sym.getGraph();
	}
}
